package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.FeedBackReplyEntity;
import com.example.demo.entity.FeedbackEntity;
import com.example.demo.entity.UserEntity;
import com.example.demo.service.FeedBackReplyService;
import com.example.demo.service.FeedbackService;
import com.example.demo.service.UserService;
import com.example.demo.tool.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "问题反馈")
public class FeedbackController {
    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private FeedBackReplyService feedBackReplyService;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private UserService userService;

    @PostMapping("add/feedback")
    @ApiOperation("增加")
    public ResponseResult addfeed(FeedbackEntity feedbackEntity){
        feedbackEntity.setCreateTime(new Date());
        feedbackService.save(feedbackEntity);
        return ResponseResult.success();
    }

    @GetMapping("allFeedback")
    @ApiOperation("全部问题反馈")
    public ResponseResult list(@ApiParam("页码，默认为1") @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                               @ApiParam("每页数量，默认为10") @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<FeedbackEntity> list = feedbackService.list(new QueryWrapper<FeedbackEntity>().orderByDesc("create_time"));
        PageInfo<FeedbackEntity> page = new PageInfo<>(list);
        return ResponseResult.success(page);
    }

    @ApiOperation("获取当前用户的反馈列表")
    @GetMapping("getAllByUserId")
    public ResponseResult getAllByUserId(Integer userId){
        FeedbackEntity feedbackEntity = feedbackService.selectById(userId);
        List<FeedBackReplyEntity> replyEntities = feedbackService.selectAllChild(feedbackEntity.getId());
        feedbackEntity.setChildList(replyEntities);
        return ResponseResult.success(feedbackEntity);
    }

    @ApiOperation("医生查看问题反馈,传入参数是问题反馈的id")
    @GetMapping("getAllByDoctor")
    public ResponseResult getAllByDoctor(Integer id){
        FeedbackEntity entity = feedbackService.getById(id);
        List<FeedBackReplyEntity> replyEntities = feedbackService.selectAllChild(entity.getId());
        entity.setChildList(replyEntities);
        return ResponseResult.success(entity);
    }

    @ApiOperation("管理员回复信息,其中parentId是回复的问题反馈的id")
    @PostMapping("addFeedBackReply")
    public ResponseResult addFeedBackReply(@RequestBody FeedBackReplyEntity feedBackReplyEntity){
        feedBackReplyService.save(feedBackReplyEntity);
        UserEntity entity = userService.getOne(new QueryWrapper<UserEntity>().eq("user_id", feedBackReplyEntity.getUserId()));
        SimpleMailMessage message=new SimpleMailMessage();
        message.setText("上号");
        message.setSubject("test");
        message.setTo(entity.getEmail());
        message.setFrom("3112822380@qq.com");
        javaMailSender.send(message);
        return ResponseResult.success();
    }

    @ApiOperation("病人回复信息,其中parentId是回复的问题反馈的id")
    @PostMapping("UseraddFeedBackReply")
    public ResponseResult UseraddFeedBackReply(@RequestBody FeedBackReplyEntity feedBackReplyEntity){
        feedBackReplyService.save(feedBackReplyEntity);
        return ResponseResult.success();
    }
}
